package cn.nfwx.tv.adapter;

import android.content.Context;
import android.support.v4.util.ArraySet;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayoutManager;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;

import cn.nfwx.tv.R;
import cn.nfwx.tv.util.LogUtil;


/**
 * 搜索历史
 * Created by zb.liu by  2017/11/2.
 */
public class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.SearchHistoryViewHolder> {
    private final String TAG = SearchHistoryAdapter.class.getSimpleName();
    private Context mContext;
    private int mSelect = 0;//选中项
    private String[] titles = new String[]{};

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public interface OnItemClickListener {
        void itemClick(RecyclerView.Adapter adapter, View view, int position, String keywork);
    }

    public SearchHistoryAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public SearchHistoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.activity_search_history_item, null);
        SearchHistoryViewHolder viewHolder = new SearchHistoryViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final SearchHistoryViewHolder holder, final int position) {
        holder.tvName.setText(titles[position]);
        ViewGroup.LayoutParams lp = holder.tvName.getLayoutParams();
        if (lp instanceof FlexboxLayoutManager.LayoutParams) {
            FlexboxLayoutManager.LayoutParams flexboxLp =
                    (FlexboxLayoutManager.LayoutParams) holder.tvName.getLayoutParams();
            flexboxLp.setFlexGrow(1.0f);
        }
        holder.tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.itemClick(SearchHistoryAdapter.this, holder.tvName, position, titles[position]);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return titles.length;
    }

    public void setNewData(ArrayList<String> newData) {
        titles = newData.toArray(titles);
        notifyDataSetChanged();
    }

    public void clearData() {
        titles = new String[]{};
        notifyDataSetChanged();
    }

    class SearchHistoryViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;

        public SearchHistoryViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.history_text);
        }
    }
}
